package com.mmall.service.impl;/**
 * Created by 果果 on 2017/8/27.
 */


import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.mmall.common.ServerResponse;
import com.mmall.dao.CategoryMapper;
import com.mmall.pojo.Category;
import com.mmall.service.ICategoryService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Set;

/**
 * @author 果果
 * @描述 商品分类实现类
 * @create 2017-08-27 17:42
 **/
@Service("iCategoryService")
public class CategoryServiceImpl implements ICategoryService {
    private org.slf4j.Logger logger= LoggerFactory.getLogger(CategoryServiceImpl.class);
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 添加品类
     * @param categoryName
     * @param parentId
     * @return
     */
    @Override
    public ServerResponse addCategory(String categoryName, Integer parentId) {
        if (parentId==null|| StringUtils.isBlank(categoryName)){
            return ServerResponse.createByErrorMessage("商品分类参数有误");
        }
        Category category=new Category();
        category.setName(categoryName);
        category.setParentId(parentId);
        category.setStatus(true);
        int addCount=categoryMapper.insert(category);
        if (addCount>0){
            return ServerResponse.createBySuccessMessage("添加品类成功");
        }
        return ServerResponse.createByErrorMessage("添加品类失败");
    }

    /**
     * 更新品类名称
     * @param categoryId
     * @param categoryName
     * @return
     */
    public ServerResponse updateCategoryName(Integer categoryId,String categoryName){
        if (categoryId==null||StringUtils.isBlank(categoryName)){
            return ServerResponse.createByErrorMessage("参数有误，更新品类名称失败");
        }
        Category category=new Category();
        category.setId(categoryId);
        category.setName(categoryName);
        //调用更新方法
        int resultCount=categoryMapper.updateByPrimaryKeySelective(category);
        if (resultCount>0){
            return  ServerResponse.createBySuccessMessage("品类更新成功");
        }
        return  ServerResponse.createBySuccessMessage("品类更新失败");
    }

    /**
     * 查询分类--平级
     * @param categoryId
     * @return
     */
    public ServerResponse<List<Category>> getChildrenPalallelCategory(Integer categoryId){
        List<Category> categoryList=categoryMapper.getChilerenPalallelCategory(categoryId);
        if (CollectionUtils.isEmpty(categoryList)){
            logger.info("未找到当前分类的子分类");
        }
        return  ServerResponse.createBySuccess(categoryList);
    }

    /**
     * 查询节点的id以及孩子节点的Id
     * @param categoryId
     * @return
     */
    public ServerResponse<List<Integer>> getDeepChildCategory(Integer categoryId){
        Set<Category> categorySet= Sets.newHashSet();
        findChildCategory(categorySet,categoryId);
        //构造返回的数据
        List<Integer>  categoryList= Lists.newArrayList();
        for (Category categoryItem:categorySet){
            categoryList.add(categoryItem.getId());
        }
        return  ServerResponse.createBySuccess(categoryList);
    }


    /**
     * 递归查询分类子节点
     * @param categorySet
     * @param categoryId
     * @return
     */
    public Set<Category> findChildCategory(Set<Category> categorySet,Integer categoryId){
        Category category=categoryMapper.selectByPrimaryKey(categoryId);
        if (category!=null){
            categorySet.add(category);
        }
        //查找子节点
        List<Category> categoryList=categoryMapper.getChilerenPalallelCategory(categoryId);
        for (Category categoryItem:categoryList){
            findChildCategory(categorySet,categoryItem.getId());
        }
        return categorySet;
    }
}
